﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace Bitcoin.Test.TestData
{
    class TxValidData : IEnumerable<TestCaseData>
    {
        public IEnumerator<TestCaseData> GetEnumerator()
        {
            var file = "TestData\\tx_valid.json";
            JArray json;
            using (var tr = new StreamReader(file))
            {
                json = JArray.Parse(tr.ReadToEnd());
            }
            foreach (JArray data in json)
            {
                if (data.Count == 1)
                {
                    Console.WriteLine(data[0].ToString());
                }
                else 
                {
                    var inputsArray = (JArray)data[0];
                    var inputs = new List<Tuple<string, int, string>>();
                    foreach (JToken inputToken in inputsArray)
                    {
                        if (inputToken.Type == JTokenType.Array)
                        {
                            var inputData = (JArray) inputToken;
                            var previousHash = (string) inputData[0];
                            var previousIndex = (int) inputData[1];
                            var previousScriptPubKey = (string) inputData[2];
                            var values = new Tuple<string, int, string>(previousHash, previousIndex,
                                previousScriptPubKey);
                            inputs.Add(values);
                        }
                        else
                        {
                            Console.WriteLine("Unexpected value '{0}'", inputToken);
                        }
                    }
                    var serializedTransaction = (string) data[1];
                    var enforceP2SH = (bool) data[2];
                    yield return new TestCaseData(inputs, serializedTransaction, enforceP2SH);                    
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

}
